﻿using System;
using System.IO;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Text;
using System.Windows.Media;
using System.Xml;
using System.Xml.Serialization;

namespace FrontDisplay
{
    /// <summary>
    /// Color theme for Front display
    /// </summary>
    [Serializable]
    public class ColorTheme
    {
        public string Name { get; set; }
        public Color TextForeground { get; set; }
        public Color TextShadow { get; set; }

        public Color Background { get; set; }
        public Color BackgroundTop { get; set; }
        public Color BackgroundBottom { get; set; }

        public ColorTheme()
        {
            TextForeground = Colors.White;
            TextShadow = Colors.Gray;
            BackgroundTop = Colors.Black;
            Background = Colors.DarkRed;
            BackgroundBottom = Colors.Black;
        }

        public string ToXmlString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<ColorTheme ");
            sb.AppendFormat("TextForeground='{0}' ", TextForeground);
            sb.AppendFormat("TextShadow='{0}' ", TextShadow);
            sb.AppendFormat("BackgroundTop='{0}' ", BackgroundTop);
            sb.AppendFormat("Background='{0}' ", Background);
            sb.AppendFormat("BackgroundBottom='{0}' ", BackgroundBottom);
            sb.AppendLine("/>");
            return sb.ToString();
        }

        public static ColorTheme CreateFromXmlString(string data)
        {
            ColorTheme colorTheme = new ColorTheme();
            colorTheme.InitFromXmlString(data);
            return colorTheme;
        }

        private void InitFromXmlString(string data)
        {
            XmlDocument xml = new XmlDocument();
            xml.LoadXml(data);

            //TODO: throw error
            if (xml.DocumentElement == null) return;

            TextForeground = (Color)ColorConverter.ConvertFromString(xml.DocumentElement.GetAttribute("TextForeground"));
            TextShadow = (Color)ColorConverter.ConvertFromString(xml.DocumentElement.GetAttribute("TextShadow"));
            BackgroundTop = (Color)ColorConverter.ConvertFromString(xml.DocumentElement.GetAttribute("BackgroundTop"));
            Background = (Color)ColorConverter.ConvertFromString(xml.DocumentElement.GetAttribute("Background"));
            BackgroundBottom = (Color)ColorConverter.ConvertFromString(xml.DocumentElement.GetAttribute("BackgroundBottom"));
        }

        protected string FileName
        {
            get
            {
                return ThemeManager.ThemesDir + Name + ".xml";
            }
        }

        public void SaveToFile()
        {
            using (var writer = new StreamWriter(FileName))
            {
                writer.Write(ToXmlString());
            }
        }

        public static ColorTheme ReadFromFile(string themeName)
        {
            ColorTheme colorTheme = new ColorTheme {Name = themeName, TextForeground = Colors.White};

            if (!File.Exists(colorTheme.FileName)) return colorTheme;
            
            using (var reader = new StreamReader(colorTheme.FileName))
            {
                colorTheme.InitFromXmlString(reader.ReadToEnd());
            }
            return colorTheme;
        }

        public override string ToString()
        {
            return Name;
        }

    }
}
